package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.*;


@WebServlet("/CargoHandler")
public class CargoHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private static String ADD = "/CargoAdd.jsp";
    private static String Edit = "/Cargoedit.jsp";
    private static String CargoRecord="/ListCargo.jsp";
    private CargoDao dao;
    
    public CargoHandler() {
        super();
        dao = new CargoDao();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";       
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listCargo")) {
			redirect = CargoRecord;
			request.setAttribute("Cargo", dao.getAllCargos());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("CargoId");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Cargo objCargo = new Cargo();
			Cargo_Dependencia objcar_dep = new Cargo_Dependencia();
			objCargo.setNombre_Cargo(request.getParameter("Nombre_Cargo"));
			objcar_dep.setDependenciaId(Integer.parseInt(request.getParameter("dependendenciaId")));
			dao.addCargo(objCargo,objcar_dep);
			redirect = CargoRecord;
			request.setAttribute("Cargo", dao.getAllCargos());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String Cargo_Id = request.getParameter("CargoId");
			int lugid = Integer.parseInt(Cargo_Id);
			dao.removeCargo(lugid);
			redirect = CargoRecord;
			request.setAttribute("Cargo", dao.getAllCargos());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String Cargo_Id = request.getParameter("CargoId");
			int pegid = Integer.parseInt(Cargo_Id);
			Cargo objCargo = new Cargo();
			objCargo.setCargoId(pegid);
			objCargo.setNombre_Cargo(request.getParameter("Nombre_Cargo"));
			request.setAttribute("Cargo", objCargo);
			redirect = CargoRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listCargo")) {
			redirect = CargoRecord;
			request.setAttribute("Cargo", dao.getAllCargos());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
